package com.yunji.product.dao.impl;

import cn.hutool.core.lang.Pair;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.google.common.base.Optional;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.common.model.cache.ProductBelongCacheDTO;
import com.yunji.product.bo.ProductListQueryBO;
import com.yunji.product.bo.ProductSelectQueryBO;
import com.yunji.product.dao.IProductDao;
import com.yunji.product.domain.ProductDO;
import com.yunji.product.domain.ProductAgentDO;
import com.yunji.product.mapper.ProductMapper;
import com.yunji.product.vo.ProductVO;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProductDaoImpl extends BaseDaoImpl<ProductMapper, ProductDO> implements IProductDao {

    @Override
    public List<ProductDO> searchProductPrice(ProductListQueryBO bo) {
        MPJLambdaWrapper<ProductDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(ProductDO.class)
                .leftJoin(ProductAgentDO.class, ProductAgentDO::getProductId, ProductDO::getId)
                .selectAs(ProductAgentDO::getCostPrice, ProductDO::getCostPrice)
                .selectAs(ProductAgentDO::getDiscountPrice, ProductDO::getDiscountPrice)
                .eq(BeanUtils.isNotNull(bo.getProductId()), ProductDO::getId, bo.getProductId())
                .eq(BeanUtils.isNotNull(bo.getAgentId()), ProductAgentDO::getAgentId, bo.getAgentId())
                .eq(BeanUtils.isNotNull(bo.getBelongId()), ProductAgentDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getUserType()), ProductAgentDO::getUserType, bo.getUserType())
                .eq(BeanUtils.isNotNull(bo.getAccreditStatus()), ProductAgentDO::getStatus, bo.getAccreditStatus())
                .eq(BeanUtils.isNotNull(bo.getGrantType()), ProductDO::getGrantType, bo.getGrantType())
                .in(BeanUtils.isNotNull(bo.getProductIds()), ProductAgentDO::getProductId,bo.getProductIds())
                .like(BeanUtils.isNotNull(bo.getProductName()), ProductDO::getProductName, bo.getProductName());
        return pageJoinList(ProductDO.class,bo,wrapper);
    }

    @Override
    public ProductBelongCacheDTO selectProductPrice(ProductSelectQueryBO bo) {
        MPJLambdaWrapper<ProductDO> wrapper = new MPJLambdaWrapper<>();
        return selectJoinOne(ProductBelongCacheDTO.class, wrapper.selectAll(ProductDO.class)
                .leftJoin(ProductAgentDO.class, ProductAgentDO::getProductId, ProductDO::getId)
                .selectAs(ProductAgentDO::getCostPrice, ProductDO::getCostPrice)
                .selectAs(ProductAgentDO::getDiscountPrice, ProductDO::getDiscountPrice)
                .eq(BeanUtils.isNotNull(bo.getProductId()), ProductDO::getId, bo.getProductId())
                .eq(BeanUtils.isNotNull(bo.getBelongId()), ProductAgentDO::getBelongId, bo.getBelongId()));
    }

    @Override
    public Pair<String, String> getProductNameAndPics(Long productId) {
        return baseMapper.getProductNameAndPics(productId);
    }

    @Override
    public int checkProductSku(ProductVO product) {
        Long count = lambdaQuery()
                .eq(ProductDO::getSku, product.getSku())
                .ne(BeanUtils.isNotNull(product.getId()), ProductDO::getId, product.getId()).count();
        return Optional.fromNullable(count).or(0L).intValue();
    }

    @Override
    public int statsOfProductType(Long... productTypeIds) {
        Long count = lambdaQuery().in(ProductDO::getProductType, productTypeIds).count();
        return Optional.fromNullable(count).or(0L).intValue();
    }

    @Override
    public int statsOfProductUnit(Long... unitIds) {
        Long count = lambdaQuery().in(ProductDO::getUnitId, unitIds).count();
        return Optional.fromNullable(count).or(0L).intValue();
    }

    @Override
    public int statsOfProductBrand(Long... brandIds) {
        Long count = lambdaQuery().in(ProductDO::getBrandId, brandIds).count();
        return Optional.fromNullable(count).or(0L).intValue();
    }

}
